<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers, ThrottlesLogins;
    //主页路由
    protected $redirectTo = '/admin/home';
    // 单位时间内最大登录尝试次数
    protected $maxAttempts = 3;
    // 单位时间值
    protected $decayMinutes = 10;
    // 支持的登录字段
    protected $supportFields = ['name', 'email'];

    public function __construct()
    {
        $this->middleware('guest:admin')->except('logout');
    }

    protected function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        foreach ($this->supportFields as $field) {
            if (empty($credentials[$field])) {
                $credentials[$field] = $credentials[$this->username()];
            }
        }
        return $credentials;
    }

    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }

    public function showLoginForm()
    {
        return view('admin.login');
    }

    protected function guard()
    {
        return Auth::guard('admin');
    }

    protected function loggedOut(Request $request)
    {
        return redirect(route('admin.login'));
    }
}
